//
//  TableViewCell.m
//  Nesting
//
//  Created by TH的Mac on 2021/5/12.
//

#import "TableViewCell.h"
#import "CollectionViewCell.h"
@interface TableViewCell () <UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic ,strong) UIView * backView;
@property (nonatomic ,strong) NSMutableArray * dataArray;
@property (nonatomic ,strong) UICollectionView * collection;

@end

@implementation TableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {

        [self setUI];
        [self setUILayout];
    }
    return self;
}

- (void)setModel:(Model *)model
{
    _model = model;
    
    [self.collection reloadData];
    
    // 这么写才成功（复用的cell 才不会同步滑动）
    [self.collection setContentOffset:CGPointMake(_model.collectionViewOffsetX, 0) animated:NO];
    
    // 下面这么写 不会成功
//    [self.collection setContentOffset:CGPointMake(_model.collectionViewOffsetX, 0)];
    
}

#pragma mark --------------- UICollectionViewDataSource  代理
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 10;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionCellID" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor greenColor];
    return cell;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat contentOffsetX = scrollView.contentOffset.x;
    self.model.collectionViewOffsetX = contentOffsetX;
}

#pragma mark  --------------  UI布局

- (void)setUI
{
    [self.contentView addSubview:self.backView];
    [self.backView addSubview:self.collection];
}

- (void)setUILayout
{
    self.backView.frame = self.frame;
    self.collection.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 70);
}

- (UIView *)backView
{
    if (!_backView) {
        _backView = [[UIView alloc] init];
        _backView.backgroundColor = [UIColor whiteColor];
    }
    return _backView;
}
- (UICollectionView *)collection
{
    if (!_collection) {

        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal; //滚动方向
        layout.minimumLineSpacing = 10;
        layout.minimumInteritemSpacing = 0;
        layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
        layout.itemSize = CGSizeMake(100, 50);
        
        _collection = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collection.showsHorizontalScrollIndicator = NO;
        _collection.delegate = self;
        _collection.dataSource = self;
        _collection.backgroundColor = [UIColor whiteColor];
        [_collection registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"CollectionCellID"];

    }
    return _collection;
}
@end
